import requests
import hashlib  # 哈希
import time

username = f'rr{int(time.time())}'
email = username + '@qq.com'


def make_md5(text):
    m = hashlib.md5()  # 创建一个md5加密工具
    m.update(str(text).encode(encoding='utf-8'))
    res = m.hexdigest()  # 生成加密后的内容
    return res


s = requests.session()
# 注册
reg_data = {
    "username": username,
    "password": "python123",
    "cpassword": "python123",
    "checkcode": "1234",
    "email": email
}
reg_url = "http://114.132.200.42:3456/user/register"
reg = s.post(url=reg_url, data=reg_data)
print(reg.json(), "注册成功")
get_username = reg_data["username"]
print(get_username)


# 登录
login_url = "http://114.132.200.42:3456/user/login"
login_data = {
    "username": get_username,
    "password": make_md5('python123'),
    "checkcode": "1234"
}
login = s.post(url=login_url, data=login_data)
print(login.json(), "登录成功")
# 获取userId和userToken
result = login.json()
get_userId = result["userId"]
get_userToken = result["userToken"]

# 创建任务
create_url = "http://114.132.200.42:3456/task/info"
create_data = {"title": "1232435qwe"}
hd = {"userId": get_userId, "userToken": get_userToken}
create = s.post(url=create_url, data=create_data, headers=hd)
print(create.json(), "创建任务成功")
result1 = create.json()
get_taskid = result1["taskid"]
print(get_taskid)

# 查询任务
query_url = "http://114.132.200.42:3456/task/info"
query_data = {"status": 0}
query = s.get(url=query_url, params=query_data, headers=hd)
print(query.json(), "查询任务成功")

# 完成任务
complete_url = "http://114.132.200.42:3456/task/info"
complete_data = {"task_id": get_taskid}
complete = s.put(url=complete_url, data=complete_data, headers=hd)
print(complete.json(), "完成任务")

# 取消完成任务
cancel_url = "http://114.132.200.42:3456/task/info"
cancel_data = {"task_id": get_taskid}
cancel = s.put(url=cancel_url, data=cancel_data, headers=hd)
print(cancel.json(), "取消完成任务")

# 删除任务
delete_url = "http://114.132.200.42:3456/task/info"
delete_data = {"task_id": get_taskid}
delete = s.delete(url=delete_url, data=delete_data, headers=hd)
print(delete.json(), "删除任务成功")

# 上传头像
img_url = "http://114.132.200.42:3456/user/uploal_action"
img_data = [
    ('pic', ('pax.jpg', open('pdx.png', 'rb')))
]
img = s.post(url=img_url, headers=hd, files=img_data)
print(img.json(), "头像上传成功")
